//
//  testSortedArray.m
//  histoval
//
//  Created by Daniel Braun on 14/06/07.
//  Copyright 2007 __MyCompanyName__. All rights reserved.
//

#import "EDSortedArray.h"
#import "HistoryVariable.h"
#import "HistoryGenericVariable.h"
#import "HistoricVariablePlot.h"
#import "FileHandleExtension.h"

int main(int argc, char *argv[])
{
	BOOL err=NO;
	NSAutoreleasePool *pool=[[NSAutoreleasePool alloc]init];
	
	// mandatory since we are starting from pure C, simmilar to carbon
	NSApplicationLoad();
	
	EDSortedArray *sa;
	
	sa=[[EDSortedArray alloc]init];
	[sa addObject:@"coucou"];
	[sa addObject:@"test"];
	[sa addObject:@"ceci est un test"];
	[sa addObject:@"troll"];
	[sa addObject:@"ebus"];
	[sa removeObject:@"troll"];
	NSString *s=[NSString stringWithCString:"ebus"];
	[sa removeObject:s];
	unsigned int i, count = [sa count];
	for (i = 0; i < count; i++) {
		NSObject * obj = [sa objectAtIndex:i];
		NSLog(@"index %d, got %@\n",i, obj);
	}
	NSEnumerator *en;
	en=[sa objectEnumerator];
	NSObject *obj;
	while ((obj=[en nextObject])) {
		NSLog(@"enumerate %@\n", obj);
	}
	[pool release];
	if (err) return 1;
	
	pool=[[NSAutoreleasePool alloc]init];

	
	HistoryVariable *myhv=[[HistoryVariable alloc]init];
	[myhv setDValue:4];
	[myhv setDValue:123 atDate:[NSDate dateWithTimeIntervalSinceNow:-30]];
	[myhv setDValue:89 atDate:[NSDate dateWithTimeIntervalSinceNow:-42]];
	[myhv setDValue:67 atDate:[NSDate dateWithTimeIntervalSinceNow:-60]];
	[myhv setDValue:-23 atDate:[NSDate dateWithTimeIntervalSinceNow:-260]];
	[myhv setDValue:-10 atDate:[NSDate dateWithTimeIntervalSinceNow:-80]];

	NSLog(@"cur val %@\n", [myhv value]);
	NSLog(@"at -80: %@\n", [myhv valueAtDate:[NSDate dateWithTimeIntervalSinceNow:-80]]);
	NSLog(@"at -81: %@\n", [myhv valueAtDate:[NSDate dateWithTimeIntervalSinceNow:-81]]);
	NSLog(@"at -79: %@\n", [myhv valueAtDate:[NSDate dateWithTimeIntervalSinceNow:-79]]);
	NSLog(@"at -80/i: %@\n", [myhv interpolatedValueAtDate:[NSDate dateWithTimeIntervalSinceNow:-80]]);
	NSLog(@"at -81/i: %@\n", [myhv interpolatedValueAtDate:[NSDate dateWithTimeIntervalSinceNow:-81]]);
	NSLog(@"at -79/i: %@\n", [myhv interpolatedValueAtDate:[NSDate dateWithTimeIntervalSinceNow:-79]]);
	NSLog(@"avg on -100:now %@\n", [myhv averageValueBetween:[NSDate dateWithTimeIntervalSinceNow:-100]
							     and:[NSDate date]]);
	NSLog(@"avg on -100:-40 %@\n", [myhv averageValueBetween:[NSDate dateWithTimeIntervalSinceNow:-100]
							     and:[NSDate dateWithTimeIntervalSinceNow:-40]]);
	NSLog(@"evol on -100:-30 %g\n", [myhv evolutionBetween:[NSDate dateWithTimeIntervalSinceNow:-100]
							     and:[NSDate dateWithTimeIntervalSinceNow:-30]]);
	
	HistoricVariablePlot *plotter=[[HistoricVariablePlot alloc] init];
	[plotter addHistoryVariable:myhv];
	[plotter setFlags:PLOT_MIN0 | PLOT_CUMUL];
	NSImage *plotImage=[plotter plotImage];
	NSBitmapImageRep *imageRep = [NSBitmapImageRep imageRepWithData:[plotImage TIFFRepresentation]];
	NSData *d=[imageRep representationUsingType: NSPNGFileType properties:nil];
	
	
	NSString *fn = [NSTemporaryDirectory() stringByAppendingPathComponent:@"toto.png"];
	NSFileHandle *fh=[NSFileHandle fileHandleForWritingAtPathCreate:fn];
		if (!fh) NSLog(@"png opening failed\n");
	else NSLog(@"writing at %@\n", fn);
	[fh writeData:d];
	[fh closeFile];
	
	fn = [NSTemporaryDirectory() stringByAppendingPathComponent:@"toto.hv"];
	BOOL ok=[NSKeyedArchiver archiveRootObject:myhv toFile:fn];
	if (ok) {
		NSLog(@"object archived\n");
	} else {
		NSLog(@"archiv failed\n");
	}
	
	[myhv release];
	myhv=nil;
	myhv=[NSKeyedUnarchiver unarchiveObjectWithFile:fn];
	if (myhv) NSLog(@"object unarchived\n");

	HistoryVariable *myhv2=[[HistoryVariable alloc]init];
	[myhv2 setDValue:99 atDate:[NSDate dateWithTimeIntervalSinceNow:-100]];
	[myhv2 setDValue:80 atDate:[NSDate dateWithTimeIntervalSinceNow:-98]];
	[myhv2 setDValue:46 atDate:[NSDate dateWithTimeIntervalSinceNow:-70]];
	[myhv2 setDValue:20 atDate:[NSDate dateWithTimeIntervalSinceNow:-10]];
	[myhv2 setDValue:5 atDate:[NSDate dateWithTimeIntervalSinceNow:-1]];
	
	plotter=[[HistoricVariablePlot alloc] init];
	[plotter addHistoryVariable:myhv];
	[plotter addHistoryVariable:myhv2];
	[plotter setFlags:PLOT_MIN0];
	plotImage=[plotter plotImage];
	imageRep = [NSBitmapImageRep imageRepWithData:[plotImage TIFFRepresentation]];
	d=[imageRep representationUsingType: NSPNGFileType properties:nil];
	[plotter release];

	
	fn = [NSTemporaryDirectory() stringByAppendingPathComponent:@"titi.png"];
	fh=[NSFileHandle fileHandleForWritingAtPathCreate:fn];
	if (!fh) NSLog(@"png opening failed\n");
	else NSLog(@"writing at %@\n", fn);
	[fh writeData:d];
	[fh closeFile];
	
	
	plotter=[[HistoricVariablePlot alloc] init];
	[plotter addHistoryVariable:myhv];
	[plotter addHistoryVariable:myhv2];
	[plotter setFlags:PLOT_MIN0 | PLOT_CUMUL];
	plotImage=[plotter plotImage];
	imageRep = [NSBitmapImageRep imageRepWithData:[plotImage TIFFRepresentation]];
	d=[imageRep representationUsingType: NSPNGFileType properties:nil];
	[plotter release];
	
	
	fn = [NSTemporaryDirectory() stringByAppendingPathComponent:@"titi2.png"];
	fh=[NSFileHandle fileHandleForWritingAtPathCreate:fn];
	if (!fh) NSLog(@"png opening failed\n");
	else NSLog(@"writing at %@\n", fn);
	[fh writeData:d];
	[fh closeFile];

	HistoryGenericVariable *ghv=[[[HistoryGenericVariable alloc]init]autorelease];
	[ghv setValue:@"toto" forKey:@"k1" atDate:[NSDate dateWithTimeIntervalSinceNow:-70]];
	[ghv setValue:@"troll" forKey:@"k2" atDate:[NSDate dateWithTimeIntervalSinceNow:-40]];
	
	NSDictionary *v=[ghv allValuesAtDate:[NSDate date] maxDateDelta:(NSTimeInterval)-100];
	NSLog(@"got v=%@\n", v);
	v=[ghv allValuesAtDate:[NSDate date] maxDateDelta:(NSTimeInterval)-50];
	NSLog(@"got v=%@\n", v);
	
	
	[pool release];
	return 0;
}